
/**
 * Project Name:manager_server
 * File Name:CommonController.java
 * Date:2019年8月30日上午10:08:51
 *
 * @author yym
 */

package com.vast.admin.controller;


import com.vast.admin.annotation.PassToken;
import com.vast.admin.entity.ContractDatum;
import com.vast.admin.service.impl.ContractDatumService;
import com.vast.common.core.domain.entity.SysUser;
import com.vast.common.utils.FileUtils;
import com.vast.common.utils.SecurityUtils;
import com.vast.common.constant.MessageCode;
import com.vast.common.core.domain.Result;
import com.vast.system.utils.OssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@Api(value = "公共相关接口", tags = { "公共接口管理模块" })
@RequestMapping("/manager/file")
public class FileUploadCommonController {

    @Autowired
    private ContractDatumService contractDatumService;

    @Autowired
    private OssUtils ossUtils;

    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PostMapping("/upload")
    @PassToken
    public Result<String> upload(@RequestParam("file") MultipartFile file , String type) {
        String filePath = FileUtils.uploadFile(file,  FileUtils.FileType.valueOf(type));
        if (StringUtils.isEmpty(filePath)) {
            return new Result<>(MessageCode.ERROR, "上传失败");
        } else {
            return new Result<>(MessageCode.SUCCESS, "上传成功", filePath);
        }
    }

    @ApiOperation(value = "文件上传到OSS", notes = "文件上传到OSS")
    @PostMapping("/uploadOSS")
    @PassToken
    public Result<String> uploadOSS(@RequestParam("file") MultipartFile file) {
        if (file != null) {
            String url = ossUtils.uploadFile(file);
            return new Result<>(MessageCode.SUCCESS, "上传成功", url);
        } else {
            return new Result<>(MessageCode.ERROR, "上传失败");
        }
    }

    @PostMapping("/uploadContractDatum")
    public Result<ContractDatum> uploadContractDatum(@RequestParam("file") MultipartFile file, @RequestParam("type") String type, @RequestParam("contractId")long contractId) {
        if (file != null) {
			SysUser manager = SecurityUtils.getLoginUser().getUser();
            String url = ossUtils.uploadFile(file);
            if (!StringUtils.isEmpty(url)) {
                Map<String, Object> map = new HashMap<>();
                map.put("type", type);
                map.put("fileUrl", url);
                ContractDatum contractDatum = new ContractDatum();
                contractDatum.setFileName(file.getOriginalFilename());
                if(FileUtils.isImage(url)){
                    contractDatum.setType(1);//图片
                }else{
                    contractDatum.setType(2);//文件
                }
                contractDatum.setOssFilePath(url);
                contractDatum.setStatus(1);
                contractDatum.setContractId(contractId);
                contractDatum.setDatumType(type);
                contractDatum.setRemark("");
                contractDatum.setValid(1);
                contractDatum.setCreateBy(manager.getUserId());
                contractDatumService.save(contractDatum);
                return new Result<ContractDatum>(MessageCode.SUCCESS, "上传成功", contractDatum);
            } else {
                return new Result<ContractDatum>(MessageCode.ERROR, "上传失败");
            }
        }else{
            return new Result<ContractDatum>(MessageCode.ERROR, "上传失败");
        }
    }

}
